package com.st.vmall.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.st.vmall.entity.Type;

public interface TypeMapper {

	/**
	 * 添加分类
	 * @param type
	 * @return
	 */
	@Insert("insert into type (name,description,parent,image,status) "
			+ "value(#{name},#{description},#{parent},#{image},#{status})")
	int insert(Type type);

	/**
	 * 根据上级分类查询分类
	 * @param parent
	 * @return
	 */
	@Select("<script>select * from type where "
			+ "<if test='parent==null'>parent is null</if> "
			+ "<if test='parent!=null'>parent=#{parent}</if> "
			+ "</script>")
	List<Type> selectByParent(@Param("parent")Integer parent);

	/**
	 * 查询所有类型信息
	 * @return
	 */
	@Select("select t1.*, t2.name as pname "
			+ "from type t1 left join type t2 on t1.parent=t2.id")
	List<Map> selectAll();

	/**
	 * 多条件组合查询
	 * @param type
	 * @return
	 */
	@Select("<script>select t1.*, t2.name as pname "
			+ "from type t1 left join type t2 on t1.parent=t2.id "
			+ "<where>"
			+ "<if test='name!=null and name.length>0'> t1.name like '%${name}%' </if>"
			+ "<if test='parent!=null'> and t1.parent = #{parent} </if>"
			+ "<if test='status!=null'> and t1.status = #{status} </if>"
			+ "</where>"
			+ "</script>")
	List<Map> selectByType(Type type);
	
	/**
	 * 根据id删除分类
	 * @param id
	 * @return
	 */
	@Delete("delete from type where id=#{id}")
	int delete(Integer id);

	/**
	 * 修改分类信息
	 * @param type
	 * @return
	 */
	@Update("update type set name=#{name}, description=#{description}, "
			+ "parent=#{parent}, image=#{image}, status=#{status} "
			+ "where id=#{id}")
	int update(Type type);

	
}
